package com.yylending.las.authority.password;

import android.text.TextUtils;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.common.AESEntity;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by laixj on 2017/5/26.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class ValidatePasswordPresenter extends Presenter<Model, ValidatePasswordPresenter.View> {
	private final static String TAG = "ValidatePasswordPresenter";

	public static class Generator implements PresenterFactory<ValidatePasswordPresenter> {
		@Override
		public ValidatePasswordPresenter create() {
			return new ValidatePasswordPresenter();
		}
	}

	public interface View extends IView {
		void validatePwdSuccess();

		void validatePwdFail(String msg);
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(ValidatePasswordPresenter.View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new ValidatePasswordModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void validatePwd(String password) {
		execute(new AESEntity(new ValidatePasswordModel.ValidatePassword(password)), ValidatePasswordModel.METHOD_VALIDATE_PASSWORD, true, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void validatePwdResult(ValidatePasswordModel.ValidatePwdResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				CLog.i(TAG, "密码验证成功");
				mView.validatePwdSuccess();
			} else {
				String msg = result.getRetMsg();
				mView.validatePwdFail(TextUtils.isEmpty(msg) ? "密码验证失败" : msg);
			}
		}
	}
}
